<?php

namespace app\api\controller;

use app\api\model\MerchantMoney as MerchantMoneyModel;

/**
 * 商品控制器
 * Class Item
 * @package app\api\controller
 */
class Merchantmoney extends Controller
{

    public function initialize()
    {
        $this->user = $this->getShangUser();   // 商户信息
    }


    /**
     * 我的钱包
     */
    public function edit()
    {
        $model = new MerchantMoneyModel;
        $date = $model->detailad($this->user['merchant_id']);
        if($date){
            return $this->renderSuccess('1000','成功',$date,'1000');
        }else{
            return $this->renderSuccess('1000','成功',0);
        }
    }


    /**
     * 钱包列表
     */
    public function lists()
    {

        $info = $this->request->post();
        if(!empty($info['statime'])&&!empty($info['endtime'])){
            $statime = strtotime(date('Y-m-d 0:0:1',strtotime($info['statime'])));
            $endtime = strtotime(date('Y-m-d 23:59:59',strtotime($info['endtime'])));
        }else{
            $statime = 0;
            $endtime = time();
        }
        $where['merchant_id'] = $this->user['merchant_id'];
        $model = new MerchantMoneyModel;
        $date = $model->listsad($where,$statime,$endtime);;

        return $this->renderSuccess('1000','成功',$date);
    }

    /**
     * 钱包列表
     */
    public function addMoney()
    {
        $info = $this->request->post();
        if(empty($info['money'])||empty($info['leixing'])||empty($info['zhanghao'])||empty($info['name'])){
            return $this->renderSuccess('1001','缺少参数');
        }
        if($this->user['money']<$info['money']){
            return $this->renderSuccess('1002','余额不足');
        }
        if($info['leixing'] == '1'){
            $where['leixing'] = '微信';
        }else{
            $where['leixing'] = '支付宝';
        }
        $where['name'] = $info['name'];
        $where['zhanghao'] = $info['zhanghao'];
        $where['shop_money'] = $info['money'];
        $where['merchant_id'] = $this->user['merchant_id'];
        $where['code'] = 1;
        $where['create_time'] = time();
        $merwhe['money'] =$this->user['money']-$info['money'];
        $merwhe['shop_money'] =$this->user['shop_money']+$info['money'];
        $model = new MerchantMoneyModel;
        $list = $model->addMoney($where,$this->user['merchant_id'],$merwhe);
        if($list){
            return $this->renderSuccess('1000','成功');
        }else{
            return $this->renderSuccess('1003','失败');
        }
    }


    /**
     * wode 客户
     */
    public function meKehu()
    {
        $info = $this->request->post();
        if(empty($info['user_id'])||empty($info['phone'])||empty($info['yushuan'])||empty($info['jieshao'])||empty($info['gongdi_address'])){
            return $this->renderSuccess('1001','缺少参数');
        }
        $where['user_id'] = $info['user_id'];
        $where['phone'] = $info['phone'];
        $where['yushuan'] = $info['phone'];
        $where['jieshao'] = $info['jieshao'];
        $where['gongdi_address'] = $info['gongdi_address'];
        $where['merchant_id'] = $this->user['merchant_id'];
        $where['create_time'] = time();
        $model = new MerchantMoneyModel;
        $list = $model->meKehu($where);
        if($list){
            return $this->renderSuccess('1000','成功');
        }else{
            return $this->renderSuccess('1003','失败');
        }
    }

    /**
     * wode 客户
     */
    public function meList()
    {
        $info = $this->request->post();
        if(empty($info['type'])){
            return $this->renderSuccess('1001','缺少参数');
        }
        $where['type'] = $info['type'];
        $where['merchant_id'] = $this->user['merchant_id'];
        $where['code'] = '1';

        $model = new MerchantMoneyModel;
        $list = $model->meList($where);
        if($list){
            return $this->renderSuccess('1000','成功',$list);
        }else{
            return $this->renderSuccess('1003','失败');
        }
    }

}
